package com.ai.ubssapp.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ai.ubssapp.annotation.HostCheck;
import com.ai.ubssapp.annotation.ParamCheck;
import com.ai.ubssapp.bean.RespEncrypt;
import com.ai.ubssapp.bean.RespInfo;
import com.ai.ubssapp.constant.Constants;
import com.ai.ubssapp.service.CasService;
import com.ai.ubssapp.util.ParamTool;
import com.alibaba.fastjson.JSON;

/**
 * cas Controller
 * 
 */
@RestController
@RequestMapping("/cas")
public class CasController extends AppController {
	
	@Resource(name = "casService")
	private CasService casService;
	
	/**
	 * 获取cas登录参数接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@ParamCheck({"staffId", "password"})
	@HostCheck({HOST_INTF_COMMON})
	@RequestMapping(value = "/login")
	public RespEncrypt login(@RequestBody Map<String, Object> inParam) throws Exception {
        RespEncrypt respEncrypt = new RespEncrypt();
		//RespInfo respInfo = new RespInfo();
		
		Map<String, Object> newParam = ParamTool.transReqParam(inParam);
		RespInfo respInfo = casService.login((String)newParam.get("staffId"), (String)newParam.get("password"), (String)newParam.get("randomNum"));
		
		respInfo.setRespCode(Constants.RESP_CODE_FAILURE);
		respInfo.setRespDesc("调用能力平台地址错误！");
		String respStr = JSON.toJSONString(respInfo); //明文的响应
		String newRespStr = ParamTool.transRespInfo(respStr); //加密后的密文响应
		respEncrypt.setRespInfo(newRespStr); //包装下
		return respEncrypt;
	}
	
	/**
	 * 获取cas登录参数接口
	 * 
	 * @return
	 * @throws Exception
	 */
	@ParamCheck
	@HostCheck({HOST_INTF_COMMON})
	@RequestMapping(value = "/login/{staffId}/{password}")
	public RespInfo login(@PathVariable String staffId, @PathVariable String password) throws Exception {
		return casService.login(staffId, password,null);
	}
	
	/**
	 * 退出cas
	 * 
	 * @return
	 * @throws Exception
	 */
	@ParamCheck
	@HostCheck({HOST_INTF_COMMON})
	@RequestMapping(value = "/logout/{loginParam}")
	public RespInfo logout(@PathVariable String loginParam) throws Exception {
		RespInfo respInfo = new RespInfo();
		if(StringUtils.isEmpty(loginParam)){
			respInfo.setRespCode(Constants.RESP_CODE_FAILURE);
			respInfo.setRespDesc("loginParam参数不能为空！");
			return respInfo;
		}
		return casService.logout(loginParam);
	}
}
